@echo off
chcp 65001 >nul
echo ====================================
echo 药品管理系统完整API测试
echo ====================================
echo.

echo 🔍 正在检查后端服务状态...
timeout /t 3 /nobreak >nul

:check_backend
curl -s http://localhost:8081/api/health >nul 2>&1
if %errorlevel% neq 0 (
    echo ❌ 后端服务未启动，正在等待...
    timeout /t 5 /nobreak >nul
    goto check_backend
)

echo ✅ 后端服务已启动！
echo.

echo ====================================
echo 1. 测试基础API接口
echo ====================================

echo 📊 健康检查:
curl -s http://localhost:8081/api/health
echo.
echo.

echo 📋 获取药品列表:
curl -s http://localhost:8081/api/drugs
echo.
echo.

echo 📦 获取库存信息:
curl -s http://localhost:8081/api/inventories
echo.
echo.

echo 💰 获取销售记录:
curl -s http://localhost:8081/api/sale-records
echo.
echo.

echo ====================================
echo 2. 测试药品CRUD操作
echo ====================================

echo 📝 创建新药品:
curl -s -X POST http://localhost:8081/api/drugs ^
  -H "Content-Type: application/json" ^
  -d "{\"name\":\"测试药品\",\"specification\":\"10mg×10片\",\"manufacturer\":\"测试厂家\",\"prescription\":false}"
echo.
echo.

echo 📋 获取更新后的药品列表:
curl -s http://localhost:8081/api/drugs
echo.
echo.

echo 🔍 获取单个药品信息 (ID: 1):
curl -s http://localhost:8081/api/drugs/1
echo.
echo.

echo ✏️ 更新药品信息 (ID: 1):
curl -s -X PUT http://localhost:8081/api/drugs/1 ^
  -H "Content-Type: application/json" ^
  -d "{\"name\":\"阿莫西林胶囊(更新)\",\"specification\":\"250mg×24粒\",\"manufacturer\":\"华润医药集团\",\"prescription\":true}"
echo.
echo.

echo 📋 验证更新结果:
curl -s http://localhost:8081/api/drugs/1
echo.
echo.

echo ====================================
echo 3. 测试库存管理
echo ====================================

echo 📦 获取库存不足药品:
curl -s http://localhost:8081/api/inventories/low-stock
echo.
echo.

echo ⏰ 获取即将过期药品:
curl -s http://localhost:8081/api/inventories/expiring
echo.
echo.

echo ====================================
echo 4. 测试JWT认证
echo ====================================

echo 🔐 用户登录获取Token:
set "token_response="
for /f "delims=" %%i in ('curl -s -X POST http://localhost:8081/api/auth/login -H "Content-Type: application/json" -d "{\"username\":\"testuser2024\",\"password\":\"12345\"}"') do set "token_response=%%i"
echo %token_response%
echo.

echo ====================================
echo 5. 数据库连接测试
echo ====================================

echo 🗄️ 检查数据库中的数据:
mysql -u root -pROOT -e "USE medicalstore; SELECT COUNT(*) as drug_count FROM drugs; SELECT name, specification FROM drugs LIMIT 3;" 2>nul
echo.

echo ====================================
echo 6. 前端连接准备
echo ====================================

echo 🌐 前端启动命令:
echo   cd medicine-management
echo   npm run dev
echo.

echo 📱 访问地址:
echo   前端: http://localhost:5173
echo   后端: http://localhost:8081/api
echo.

echo 🔑 测试账号:
echo   用户名: testuser2024
echo   密码: 12345
echo.

echo ====================================
echo 测试完成报告
echo ====================================

echo ✅ 后端API服务正常
echo ✅ 药品CRUD操作可用
echo ✅ 库存管理功能可用
echo ✅ JWT认证机制正常
echo ✅ 数据库连接正常
echo.

echo 🎯 下一步操作:
echo 1. 启动前端: cd medicine-management ^&^& npm run dev
echo 2. 访问: http://localhost:5173
echo 3. 登录测试: testuser2024 / 12345
echo 4. 测试药品管理功能
echo.

pause 